<?php

// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use service\LogService;
use think\Db;

/**
 * 模板管理
 * Class Temp
 * @package app\admin\controller
 * @author pp <124910168@qq.com>
 * @date 2018/07/23
 */
class Temp extends BasicAdmin
{
    public $table = 'temp';

    public function index()
    {
        $this->title = '模板';
        $get = $this->request->get();
        $db = Db::name($this->table)->order("create_at desc");
        foreach (['title', 'path', 'status'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%")->order("create_at desc");
            }
        }
        $data = parent::_list($db, true, false);
        return $this->fetch('', $data);
    }


    public function add()
    {        
        $title = isset($params['id']) ? '编辑模板' : '新增模板';
        $this->assign('title', $title);
        return $this->_form($this->table, 'form');
    }
    
    // public function add()
    // {
    //     $params = $this->request->param();
    //     if ($this->request->isPost()) {//处理新增保存 或者 编辑保存
    //         unset($params['spm']);
    //         $vo = $params;
    //         $user = session('user');
    //         $vo['create_by'] = $user['id'];
    //         array_shift($vo);

    //         if(isset($vo['id'])) {//编辑后提交
    //             $vo['update_at'] = time();//修改时间
    //             db($this->table)->update($vo);
    //             $relation_object_id = $vo['id'];
    //         }else {
    //             $vo['update_at'] = $vo['create_at'] = time();
    //             $relation_object_id=db($this->table)->insertGetId($vo);
    //         }
    //         $relation_object_id !== false ? $this->success('恭喜，保存成功哦！', url('/').'admin.html#'.url('temp/index').'?spm='.$this->request->param('spm')) : $this->error('保存失败，请稍候再试！');
    //     }else {
    //         $data = [];
    //         if(isset($params['id'])) {
    //             $title = '编辑模板';
    //             $data = db($this->table)->find($params['id']);
    //         }else {
    //             $title = '新增模板';
    //         }
    //         $this->assign('title', $title);

    //         return $this->fetch('form', ['vo' => $data]);
    //     }
    // }

    /**
     * 删除
     */
    public function del() {
        $doc_id = $this->request->param('id');

        if (DataService::update($this->table)) {
            DataService::update($this->table, ['id' => $doc_id]); // 删除内容
            LogService::write('系统设置', '模板删除成功');
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用 0
     */
    public function forbid() {
        if (DataService::update($this->table)) {
            $this->success("状态改为禁用成功！", '');
            LogService::write('系统设置', '模板禁用成功');
        }
        $this->error("状态改为禁用失败，请稍候再试！");
    }

    /**
     * 开启 1
     */
    public function resume() {
        if (DataService::update($this->table)) {
            LogService::write('系统设置', '模板启用成功');
            $this->success("状态改为开启成功！", '');
        }
        $this->error("状态改为开启失败，请稍候再试！");
    }


}
